<#include "../common/_public.html"/>
<link rel="stylesheet" href="${base}/layui/css/layui.css"  media="all">
<fieldset class="layui-elem-field">
<#if type == 'add'><legend>新增用户</legend></#if>
<#if type == 'edit'><legend>编辑用户</legend></#if>
    <form class="layui-form layui-form-pane" id="LAY_user_form" >
        <input type="hidden" value="${userId!}" name="uId">
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>登录名</label>
            <div class="layui-input-inline">
                <input type="text" name="userName" <#if type == 'edit' || type == 'addRole' >disabled</#if>   lay-verify="loginName" value="${entity.userName!}" placeholder="请输入登录名" autocomplete="off" class="layui-input <#if type == 'edit' || type == 'addRole'>layui-disabled</#if>">
            </div>
        </div>
        <#if type == 'add'>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="uPassword" <#if type == 'addRole' >disabled</#if> lay-verify="pass" value="${entity.uPassword!}" placeholder="请输入密码" autocomplete="off" class="layui-input <#if type == 'addRole'>layui-disabled</#if>">
                </div>
            </div>
        </#if>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                <input type="text" name="uPhone" <#if type == 'addRole' >disabled</#if>   lay-verify="phone" value="${entity.uPhone!}" placeholder="请输入手机号" autocomplete="off" class="layui-input <#if type == 'addRole'>layui-disabled</#if>">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input type="text" name="uEmail" <#if type == 'addRole' >disabled</#if>  lay-verify="email"  value="${entity.uEmail!}" placeholder="请输入邮箱" autocomplete="off" class="layui-input <#if type == 'addRole'>layui-disabled</#if>">
            </div>
        </div>
        <#if type == 'addRole'>
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>选择角色</label>
                <div class="layui-input-inline">
                    <select name="role">
                        <option value="">--请选择--</option>
                                <#list roles as role>
                                        <option value="${role.id!}">${role.name!}</option>
                                </#list>
                    </select>
                </div>
            </div>
        </#if>
        <div class="layui-form-item">
            <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="uesrForm" >保存</button>
                <button class="layui-btn  layui-btn-danger"  onclick="closedView()">关闭</button>
            </div>
        </div>
    </form>
</fieldset>
<script src="${base}/layui/layui.js" charset="UTF-8"></script>
<script>
    layui.use(['element','rate','layer','form'], function(){
        var element = layui.element,
                rate = layui.rate,
                layer = layui.layer,
                $ = layui.$,
                form = layui.form;
        form.verify({
            loginName:function (value) {
                if(!new RegExp("^[a-zA-Z]{1}").test(value)){
                    return '用户名第一位不能是数字或特殊字符';
                }
                if(value.length < 5){
                    return '用户名最少包含5个字符';
                }
                if (!new RegExp("^[a-zA-Z]{1}[a-zA-Z0-9]{4,}$").test(value)){
                    return '用户名不能含有特殊字符';
                }
            }
            ,pass: [/(.+){6,18}$/, '密码必须6到12位']
            ,phone:function (value) {
                if(value.length != 0) {
                    if (!new RegExp("^1[3|7|5|8][0-9]\\d{8}$").test(value)) {
                        return '请输入正确的手机号';
                    }
                }
            }

            ,email:function (value) {
                if(value.length != 0) {
                    if (!new RegExp("^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$").test(value)) {
                        return '请输入正确的邮箱地址';
                    }
                }
            }
        });

        //监听提交
        form.on('submit(uesrForm)', function(data){
            $.ajax({
                url:'${base}/system/user_saveAndUpdate.do',
                type:'post',
                data:$("#LAY_user_form").serialize(),
                dataType:"json",
                success:function(result) {
                    if(result.statusCode == 200){
                        layer.msg(result.message,{
                            icon:1,
                            time:1500,
                            offset: '20px'
                        }, function(){
                            var index = parent.layer.getFrameIndex(window.name);
                            window.parent.location.reload();
                            parent.layer.close(index);
                        })
                    }else if(result.statusCode == 300){
                        layer.msg(result.message,{
                            icon:2,
                            time:2000,
                            offset: '20px'
                        });
                    }
                }
            });
            return false;
        });

    });

    function closedView() {
        //alert("点击了关闭按钮");
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
</script>